package com.oauth.google.server.controller;

import java.util.List;

import org.gwtwidgets.server.spring.GWTSpringController;
import org.springframework.beans.factory.annotation.Autowired;

import com.oauth.common.server.util.OAuthRequestUtil;
import com.oauth.google.client.beans.gmail.GoogleMail;
import com.oauth.google.client.beans.gmail.GoogleMailModel;
import com.oauth.google.client.beans.orkut.HttpQueryString;
import com.oauth.google.client.exception.GoogleOAuthException;
import com.oauth.google.client.rpc.gmail.GMailRemoteService;
import com.oauth.google.server.service.GmailOAuthService;
import com.oauth.logging.OAuthLogger;

public class GoogleMailController extends GWTSpringController implements GMailRemoteService {

	@Autowired
	private GmailOAuthService gmailOAuthService;

	private OAuthLogger logger = OAuthLogger.getLogger(this.getClass());

	private static final long serialVersionUID = -5758464341967618224L;

	/**
	 * @param pGmailOAuthService
	 */
	public void setMailOAuthService(GmailOAuthService pGmailOAuthService) {
		gmailOAuthService = pGmailOAuthService;
	}

	@Override
	public GoogleMailModel getGoogleMail(HttpQueryString pQueryString) throws GoogleOAuthException {
		GoogleMailModel model = new GoogleMailModel();
		Long memberId = OAuthRequestUtil.getMemberId(getThreadLocalRequest());
		try {
			List<GoogleMail> mailList = gmailOAuthService.getGMail(memberId);
			model.setMailList(mailList);
			return model;
		} catch (Exception e) {
			logger.error("Error in fetching google mails", e);
			throw new GoogleOAuthException();
		}
	}

}
